import { Component, inject, OnInit } from '@angular/core';
import { ProgressDto, SignInput, SignService } from '@proxy/signs';
import { ConfigStateService } from '@abp/ng.core';
import { IdCardService } from '../services/id-card.service';
import { SignRecordDto } from '@proxy/dto';
import { ToasterService } from '@abp/ng.theme.shared';

interface ProgressInfo extends ProgressDto {
  undone: number;
  percent: number;
}

interface SignInfo {
  recordTime?: string;
  idNumber?: string;
  personName?: string;
  photo?: string;
}

interface CardInfo {
  idNumber?: string;
  personName?: string;
  photo?: string;
}

const emptySignInfo: SignInfo = { personName: '', idNumber: '', recordTime: '', photo: '' };

@Component({
  selector: 'app-card-sign',
  templateUrl: './card-sign.component.html',
  styleUrl: './card-sign.component.scss'
})
export class CardSignComponent implements OnInit {
  private _signService = inject(SignService);
  private _stateService = inject(ConfigStateService);
  private _cardService = inject(IdCardService);
  private _toasterService = inject(ToasterService);

  appTitle$ = this._stateService.getSetting$('SignChannel.AppTitle');

  progress: ProgressInfo = { total: 0, done: 0, undone: 0, percent: 0 };
  signInfo: SignInfo = { personName: '', idNumber: '', recordTime: '2000-01-01 04:05:06' };

  idNumberAutoSource: Array<string> = [];

  ngOnInit() {
    this.loadProgress();
  }

  loadProgress() {
    this._signService.getProgress().subscribe(data => this.setProgress(data));
  }

  clear() {
    this.signInfo = { ...emptySignInfo };
  }

  delayClear() {
    setTimeout(() => this.clear(), 5000);
    setTimeout(() => this._cardService.start(), 1000);
  }

  immediateClear() {
    this.clear();
    this._cardService.start();
  }

  handleReadCard(card: CardInfo) {
    this.signInfo.idNumber = card.idNumber;
    this.signInfo.personName = card.personName;
    this.signInfo.photo = card.photo;
    this.signInfo.recordTime = '';

    this._cardService.stop();
    this.submit();
  }

  handleInputCard() {
    this.signInfo.personName = '';
    this.signInfo.photo = '';
    this.signInfo.recordTime = '';
  }

  handleEnterCard() {
    this.submit();
  }

  submit() {
    const { idNumber, personName } = this.signInfo;
    if (!idNumber) {
      return;
    }
    const input = { idNumber, personName } as SignInput;
    this._signService.add(input).subscribe((data: SignRecordDto) => {
      if (data.time == null) {
        this._toasterService.error('没有合适的签到时间');
        this.immediateClear();
        return;
      }

      if (data.person == null) {
        this._toasterService.error('您不是考勤人员，请联系管理员');
        this.immediateClear();
        return;
      }

      this.signInfo.recordTime = data.recordTime;
      this.signInfo.personName = data.personName;
      this.delayClear();
    });
  }

  private setProgress({ total, done }: ProgressDto) {
    const undone = total - done;
    const percent = Math.floor((done / total) * 100 * 100) / 100;
    this.progress = { total, done, undone, percent };
  }
}
